package cn.circle.system.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import cn.circle.commom.controller.BackendBaseController;
import cn.circle.commom.model.DataGrid;
import cn.circle.system.model.SysCustomer;
import cn.circle.system.model.SysUser;
import cn.circle.system.pagemodel.PageHelper;
import cn.circle.system.service.ISysUserService;
import cn.circle.system.util.ReturnInfo;
import cn.circle.system.util.MD5;

@Controller
@EnableWebMvc
@RequestMapping("/manage/sysUser")
public class SysUserController extends BackendBaseController{
	
	private static final Log log = LogFactory.getLog(SysUserController.class);
	
	@Autowired
	private ISysUserService sysUserService;
	
	/**
	 * 跳转到User首页
	 */
	@RequestMapping(value = "/index")
	public String sysUserIndex() {
		return "/WEB-INF/pages/system/user/sys_User_list.jsp";
	}
	
	/**
	 * 查询用户信息
	 */
	@ResponseBody
	@RequestMapping(value = "/dataGrid")
	public DataGrid dataGrid(@ModelAttribute SysUser sysUser,PageHelper ph) {
		DataGrid dg = sysUserService.datagrid(ph,sysUser);
		return dg;
	}
	/**
	 * 跳转至 sysUser_au页(添加和修改共用一个编辑页)
	 */
	@RequestMapping(value = "/add")
	public String add(Map<String, Object> map) {
		log.info("----Ok----");
		map.put("saveUrl", "/manage/sysUser/addSave");
		return "/WEB-INF/pages/system/user/sys_User_au.jsp";
	}
	/**
	 * 跳转至 sysUser_au页
	 */
	@RequestMapping(value = "/edit")
	public String edit(String id, Map<String, Object> map) {
		log.info(id);
		System.out.println("//////"+id);
		map.put("saveUrl", "/manage/sysUser/editSave");
		if (id != null) {
			List<SysUser> model = sysUserService.getUser(id);
			log.info(model.get(0));
			map.put("model", model.get(0));
		}
		return "/WEB-INF/pages/system/user/sys_User_au.jsp";
	}
	/**
	 * 改变状态status
	 */
	@ResponseBody
	@RequestMapping(value = "/remove")
	public ReturnInfo remove(String id, ReturnInfo result) {
		
		sysUserService.remove(id);
		System.out.println("1");
		return result;
	}
	/**
	 * 删除若干条数据
	 */
	@ResponseBody
	@RequestMapping(value = "/delete")
	public ReturnInfo delete(String id, ReturnInfo result) {
		if(id !=null){
			try {
				sysUserService.delete(id);
			} catch (Exception e) {
				log.info("删除失败"+ e.getMessage());
				result.setMsg("删除失败！");
				result.setSuccess(1);
			}
		}
		return result;
	}
	/**
	 * 获得客户详细资料
	 */
	@RequestMapping(value = "/queryDepDetail")
	public String queryCustDetail(String depId, Map<String, Object> map) {
		log.info(depId);
		if (depId != null) {
			List<SysUser> model = sysUserService.getUser(depId);
			map.put("model", model.get(0));
		}
		return "/WEB-INF/pages/system/user/sys_User_detail.jsp";
	}
	/**
	 * 保存添加数据
	 */
	@ResponseBody
	@RequestMapping(value = "/addSave")
	public ReturnInfo addSave(String passWord,@Valid SysUser sysUser, BindingResult b, ReturnInfo result) {
		if (b.getErrorCount() > 0) { // 数据校验
			for (FieldError error : b.getFieldErrors()) {
				log.info(error.getField() + ":" + error.getDefaultMessage());
				result.setMsg(error.getDefaultMessage());
			}
			result.setSuccess(1);
		} else {
			passWord=MD5.md5(passWord);
			if (sysUser != null) {
				//log.info(sysCustomer.getEmail());
				try {
					sysUser.setPassWord(passWord);
					sysUserService.save(sysUser);
				} catch (Exception e) {
					log.info("保存失败" + e.getMessage());
					result.setMsg("保存失败！");
					result.setSuccess(1);
				}
			}
		}

		return result;
	}
	/**
	 * 保存修改的数据
	 */
	@ResponseBody
	@RequestMapping(value = "/editSave")
	public ReturnInfo editSave(@Valid SysUser sysUser, BindingResult b, ReturnInfo result) {
		if (b.getErrorCount() > 0) { // 数据校验
			for (FieldError error : b.getFieldErrors()) {
				log.info(error.getField() + ":" + error.getDefaultMessage());
				result.setMsg(error.getDefaultMessage());
			}
			result.setSuccess(1);
		} else {
			if (sysUser != null) {
				try {
					sysUserService.update(sysUser);
					//sysCustomerService.saveOrUpdate(sysCustomer);
				} catch (Exception e) {
					log.info("保存失败" + e.getMessage());
					result.setMsg("保存失败！");
					result.setSuccess(1);
				}

			}
		}

		return result;
	}
}
